﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace JSExcel
{
    /// <summary>
    /// Server 的摘要说明
    /// </summary>
    public class Server : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                context.Response.ContentType = "application/json;charset=utf-8";
                string strFuntion = context.Request.Form["FunctionName"];
                if (!string.IsNullOrEmpty(strFuntion))
                {
                    switch (strFuntion.ToLower())
                    {
                        case "getexcel":
                            string excelFile = context.Request.Form["FileName"];
                            var excelRT = new RTData<string>();
                            excelRT.Status = RTStatus.Success;
                            excelRT.Message = GetExcel(excelFile);
                            HttpContext.Current.Response.Write(excelRT.ToString());
                            break;
                    }
                }
            }
            catch (Exception e)
            {
                var rt = new RTData<string>();
                rt.Status = RTStatus.Error;
                rt.Message = e.Message;
                HttpContext.Current.Response.Write(rt.ToString());
            }
            HttpContext.Current.Response.End();
        }

        private string GetExcel(string fileName)
        {
            WorkBook wb = new WorkBook();
            wb.LoadExcel(HttpContext.Current.Server.MapPath("~/upload/" + fileName));
            return wb.ToJson();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}